/**
* This function set default source location_data. we need it in every URL request
*/
var __location_src='';
function __AB_PDoremi_RequestManager_SetDefaultSource(location_src){
	__location_src=location_src;
}

function _abpv_showLoading(){
	jQuery('#abpv_loadingDiv').fadeIn();
}

function _abpv_hideLoading(){
	jQuery('#abpv_loadingDiv').fadeOut();
}

jQuery(document).ready(function(){

	// auto center the loading
	jQuery.fn.centerScreen = function(loaded) { 
		var obj = this; 
		if(!loaded) { 
			obj.css('top', $(window).height()/2- this.height()/2); 
			obj.css('left', $(window).width()/2- this.width()/2); 
			$(window).resize(function() 
			{ obj.centerScreen(!loaded); }); 
		} else { 
			obj.stop(); 
			obj.animate({ top: $(window).height()/2 - this.height()/2, left: $(window).width()/2-this.width()/2}, 200, 'linear'); 
		} 
	};

	// center the loading         
	//_abpv_centerLoading();
	jQuery("#abpv_loadingDiv").centerScreen(true);
	$(window).bind('resize', function() {
		jQuery("#abpv_loadingDiv").centerScreen(false);
	});
	$(window).bind('scroll', function() {
		jQuery("#abpv_loadingDiv").centerScreen(false);
	});
});

function __AB_PDoremi_RequestManager_Init(selector){
	jQuery(document).ready(function(){

		$('#'+selector).datagrid({
			pagination: true, 
			nowrap: false,
			url: link_url,
			idField: 'id',
			height: 800,
			columns:[[
			{field:'id',title:'ID',width:40 },
			{field:'fullname',title:'Name',width:150},
			{field:'email',title:'Email',width:120},
			{field:'phone',title:'Phone',width:85},
			{field:'from_date',title:'From date',width:110, sortable: true},
			{field:'time_length',title:'Duration',width:110, sortable: true, formatter: function(value){
				var ret="";
				value=parseInt(value);
				var h=Math.floor(value/60);
				var m=(value%60);
				ret+=h+" hour(s)";
				if (m>0)
					ret+=" "+m+" min(s)";
				return ret;
			}},
			{field:'request_date',title:'Request date',width:110, sortable: true},
			{field:'content',title:'Request Content',width:130, sortable:true},
			{field:'approve',title:'Approve',width:70, sortable: true, formatter:function(value){
					if (value == 0)
						return lang_approve_state0;
					else if (value == 1)
						return lang_approve_state1;
					else if (value == 2)
						return lang_approve_state2;
			}},
			{field:'approve_name',title:'Approver',width:80, sortable: true},
			{field:'approve_date',title:'Approve Date',width:130, sortable: true}, 
			{field:'not_approve_reason',title:'Approve Reason',width:130},
			{field:'cancel_request',title:'Cancel',width:130}
			]],
			onDblClickRow: function(rowIndex, rowData){
				__AB_RequestManager_Approve("RequestManager");
			},
			onBeforeLoad: function(param){
				jQuery(this).datagrid("options").url=link_url;
			}
		});

		$('#dialog_Approve').dialog({
			title: lang_dialog_approve_title,
			modal:true , 
			width: 200, height: 260,
			buttons:[{
				text:'OK',
				iconCls:'icon-ok',
				handler:function(){
					var approve_value=jQuery("#dialog_approve_select").val();
					var approve_reason=jQuery("#approve_reason").val();
					// send request ajax
					var selected=jQuery('#RequestManager').datagrid('getSelected');

					_abpv_showLoading();

					jQuery.post("/index.php", {
						module: 'requestmodule',
						action: 'request_approve',
						src: __location_src,
						id: selected.id,
						approve: approve_value,
						approve_reason: approve_reason,
						ajax_action: 1,
						t: Math.random()
					}, function (data){
						_abpv_hideLoading();
						// close the dialog
						jQuery('#dialog_Approve').dialog("close");
						// reload datagrid
						jQuery('#RequestManager').datagrid('reload');
					});
				}
			},{
				text:'Cancel',
				handler:function(){
					$('#dialog_Approve').dialog('close');
				}
			}]
		}).dialog("close");
	});
}


function __AB_RequestManager_EditEvent(selector_name){

	var node = $('#'+selector_name).datagrid('getSelected');
	var url="";
	if (node){
		url="/index.php?module=calendarmodule&action=edit&id="+node.id;
		url+="&src="+__location_src;
		//alert(url);
		window.location=url;
	}
	// 
}

function __AB_RequestManager_DeleteEvent(selector_name){

	var node = $('#'+selector_name).datagrid('getSelected');
	var url="";
	if (node){
		url="/index.php?module=requestmodule&action=delete&id="+node.id;
		url+="&src="+__location_src;
		//alert(url);
		window.location=url;
	}
	// 
}
function __AB_RequestManager_CancelRequest(selector_name){

	var node = $('#'+selector_name).datagrid('getSelected');
	var url="";
	if (node){
		_abpv_showLoading();
		jQuery.post("/index.php", {
			module: 'requestmodule',
			action: 'request_cancel',
			src: __location_src,
			id: node.id,
			ajax_action: 1,
			t: Math.random()
		}, function (data){
			_abpv_hideLoading();
			// reload datagrid
			jQuery('#RequestManager').datagrid('reload');
		});
	}
	// 
}


function __AB_RequestManager_AddCalendar(selector_name){

	var node = $('#'+selector_name).datagrid('getSelected');
	var url="";
	if (node){
		_abpv_showLoading();
		jQuery.post("/index.php", {
			module: 'requestmodule',
			action: 'request_addcalendar',
			src: __location_src,
			id: node.id,
			ajax_action: 1,
			t: Math.random()
		}, function (data){
			data=ajax_string_dim(data);
			if (data=="")
				alert("Đưa yêu cầu vào lịch không thành công");
			else
				alert("Đưa yêu cầu vào lịch thành công");
			_abpv_hideLoading();
		});
	}
	// 
}

function __AB_RequestManager_Approve(selector_name){
	var node = $('#'+selector_name).datagrid('getSelected');
	var url="";
	if (node){
		jQuery("#dialog_approve_select").val(node.approve_bk);
		jQuery("#approve_reason").val(node.not_approve_reason);
		jQuery('#dialog_Approve').dialog("open");
	}
}